<template>
    <div>
        <div class="operarea">
            <div class="seach">
                <el-input v-model="seachForm.name" :placeholder="$t('name')" clearable size="mini"></el-input>
                <el-button size="mini" icon="el-icon-search" @click="getData"></el-button>
            </div>
            <div class="btnList">
                <el-button size="mini" @click="showAdd">{{$t('add')}}</el-button>
            </div>
        </div>
        <el-card class="cardBox">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>首页</el-breadcrumb-item>
                <el-breadcrumb-item>绣花库管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-table style="width: 100%" :data="embroTab" stripe v-adaptive="{bottomOffset: 85}" height="100">
                <el-table-column prop="name" :label="$t('name')">
                </el-table-column>
                <el-table-column prop="cloType" :label="$t('type')">
                </el-table-column>
                <el-table-column prop="patternName" label="版型">
                </el-table-column>
                <el-table-column prop="attribute" :label="$t('attribute')">
                </el-table-column>
                <el-table-column :label="$t('image')">
                    <template v-slot="scope">
                        <el-image v-if="scope.row.imgUrl !== '' && scope.row.imgUrl !== null" :src="scope.row.imgUrl" class="feelImg" :preview-src-list="getSrcList(scope.$index)"></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="cole" label="分配面料">
                    <template v-slot="scope">
                        <el-button class="allot" size="mini" @click="setItemEmb(scope.row.id)">分配面料</el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="address" width="150" :label="$t('operation')">
                    <template v-slot="scope">
                        <el-button @click.stop="showModify(scope.row)" size="mini">编辑</el-button>
                        <el-button @click.stop="showDel(scope.row.id)" size="mini">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination small @size-change="handleSizeChange" @current-change="handleCurrentChange" :pager-count="11" :current-page.sync="seachForm.pageNo" :page-sizes="[10, 20, 30, 40]" :page-size="seachForm.pageSize" layout="sizes, prev, pager, next" :total="total"></el-pagination>
        </el-card>
        <!-- 添加 -->
        <el-dialog :title="$t('add')" :visible.sync="addDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="addDlogClose(addDataForm)">
            <el-form :model="addDataForm" :rules="addRules" :ref="addDataForm" class="motuoForm">
                <el-form-item :label="$t('name')" label-width="80px" prop="id">
                    <el-select v-model="embItemId" filterable clearable :placeholder="$t('plsent')" @change="setAddIdAndName">
                        <el-option v-for="item in embGrooup" :key="item.uuid" :label="item.name" :value="item.uuid + '-' + item.name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="类别" label-width="80px" prop="cloType">
                    <el-select v-model="addDataForm.cloType" clearable :placeholder="$t('plsent')">
                        <el-option label="大袍" :value="0"></el-option>
                        <el-option label="kaften" :value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item v-if="addDataForm.cloType == 1" label="版型" label-width="80px" prop="cloType">
                    <el-input class="styleInput" v-model="modelName" readonly />
                    <el-button class="styleBtn" type="primary" size="mini" @click="checkModel">选择</el-button>
                </el-form-item>
                <el-form-item label="绣花底布" label-width="80px" prop="underlayFabId">
                    <el-cascader v-model="addDataForm.underlayFabId" :options="fabGroup" filterable clearable :show-all-levels="false" :props="{ label: 'name', value: 'id', children: 'fabrics', emitPath: false }"></el-cascader>
                </el-form-item>
                <el-form-item :label="$t('attribute')" label-width="80px" prop="attribute">
                    <el-input v-model="addDataForm.attribute" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('image')" label-width="80px">
                    <el-upload class="upload-demo" action="/sys/image/uploadImage" :on-success="handleImg" :on-remove="handleRemove" :limit="1" :on-exceed="scotland" :file-list="fileList" list-type="picture" ref="sunsetstrip">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png/jepg文件，且不超过5MB</div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="addData(addDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 修改 -->
        <el-dialog :title="$t('modify')" :visible.sync="modifyDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="modifyDlogClose(modifyDataForm)">
            <el-form :model="modifyDataForm" :rules="modifyRules" :ref="modifyDataForm" class="motuoForm">
                <el-form-item label="类别" label-width="80px" prop="cloType">
                    <el-select v-model="modifyDataForm.cloType" clearable :placeholder="$t('plsent')">
                        <el-option label="大袍" value="0"></el-option>
                        <el-option label="kaften" value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item v-if="modifyDataForm.cloType == 1" label="版型" label-width="80px" prop="cloType">
                    <el-input class="styleInput" v-model="modelName" readonly />
                    <el-button class="styleBtn" type="primary" size="mini" @click="checkModel">选择</el-button>
                </el-form-item>
                <el-form-item label="绣花底布" label-width="80px" prop="underlayFabId">
                    <el-cascader v-model="modifyDataForm.underlayFabId" :options="fabGroup" filterable clearable :show-all-levels="false" :props="{ label: 'name', value: 'id', children: 'fabrics', emitPath: false }"></el-cascader>
                </el-form-item>
                <el-form-item :label="$t('attribute')" label-width="80px" prop="attribute">
                    <el-input v-model="modifyDataForm.attribute" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('image')" label-width="80px">
                    <el-upload class="upload-demo" action="/sys/image/uploadImage" :on-success="handleImg" :on-remove="handleRemove" :limit="1" :on-exceed="scotland" :file-list="fileList" list-type="picture" ref="sunsetstrip">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png/jepg文件，且不超过5MB</div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="modifyDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="modifyData(modifyDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 删除 -->
        <el-dialog :title="$t('hint')" :visible.sync="delDialogVisible" width="420px" class="smallDlog" :close-on-click-modal="false">
            <span>{{$t('confirmdel')}}</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="delData">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 面料分配 -->
        <el-dialog title="面料分配" class="smallfhDlog" :visible.sync="embDialogVisible" width="1000px" @close="embDialogVisibleClosed" :modal-append-to-body="false">
            <el-form :model="editEmbForm" :ref="editEmbForm" class="motuoForm">
                <el-form-item label="面料" label-width="70px" prop="underlayFabId">
                    <el-cascader v-model="selectEmbId" :options="embForm" key="id" :props="{ label: 'name', value: 'id', children: 'fabrics', multiple: true, disabled: 'isPar' }" clearable></el-cascader>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="embDialogVisible = false" v-preventReClick>{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="editemb(editEmbForm)" v-preventReClick>{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- kaften版型列表 -->
        <el-dialog title="版型列表" :visible.sync="modelDialogVisible" width="1000px" class="styleDlog">
            <div class="operarea">
                <div class="seach">
                    <el-input v-model="seachModelForm.name" :placeholder="$t('name')" clearable size="mini"></el-input>
                    <el-button size="mini" icon="el-icon-search" @click="getModelData"></el-button>
                </div>
            </div>
            <el-card class="cardBox">
                <el-table style="width: 100%" :data="collarTab" stripe v-adaptive="{bottomOffset: 85}" height="100" highlight-current-row @current-change="handleModelChange">
                    <el-table-column prop="name" :label="$t('name')">
                    </el-table-column>
                    <el-table-column prop="bh" :label="$t('serialnumber')">
                    </el-table-column>
                    <el-table-column :label="$t('image')">
                        <template v-slot="scope">
                            <el-image v-if="scope.row.imgUrl !== '' && scope.row.imgUrl !== null" :src="scope.row.imgUrl" class="feelImg" :preview-src-list="getModelSrcList(scope.$index)"></el-image>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination
                    small
                    @size-change="handleModelSizeChange"
                    @current-change="handleModelCurrentChange"
                    :pager-count="11"
                    :current-page.sync="seachModelForm.pageNo"
                    :page-sizes="[10, 20, 30, 40]"
                    :page-size="seachModelForm.pageSize"
                    layout="sizes, prev, pager, next"
                    :total="clothesTotal">
                </el-pagination>
                </el-card>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="modelDialogVisible = false">已选择</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {
    getEmbroideryList,
    getGroupTree,
    getEmbroideryNotInElement,
    addEmbroidery,
    updateEmbroidery,
    delEmbroidery,
    getFabricWithEmbroidery,
    distributionFabric,
    getRobesElementAll
} from '@/libs/reqlist.js';
export default {
    data () {
        return {
            seachForm: {
                name: '',
                attribute: '',
                patternId: '',
                cloType: '',
                pageNo: 1,
                pageSize: 10
            },
            total: 0,
            embroTab: [],
            addDialogVisible: false,
            // 绣花id
            embItemId: '',
            addDataForm: {
                id: '',
                underlayFabId: '',
                name: '',
                attribute: '',
                imgUrl: '',
                patternId: '',
                cloType: ''
            },
            addRules: {
                id: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'change' }
                ],
                underlayFabId: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'change' }
                ],
                cloType: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'change' }
                ]
            },
            modifyDialogVisible: false,
            fileList: [],
            modifyDataForm: {
                id: '',
                underlayFabId: '',
                imgUrl: '',
                attribute: '',
                patternId: '',
                cloType: ''
            },
            modifyRules: {
                underlayFabId: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'change' }
                ]
            },
            delItemId: '',
            delDialogVisible: false,
            doType: 0,
            imgList: [],
            // 面料数据
            fabGroup: [],
            // 绣花数据
            embGrooup: [],
            // 面料分配
            embDialogVisible: false,
            embForm: [],
            editEmbForm: {
                id: '',
                fabricIds: []
            },
            selectEmbId: [],
            modelDialogVisible: false,
            seachModelForm: {
                deleteStatus: 0,
                pageNo: 1,
                pageSize: 10,
                name: '',
                bh: '',
                type: 'kftPattern',
                cloType: '1'
            },
            imgModelList: [],
            collarTab: [],
            clothesTotal: 0,
            modelName: ''
        }
    },
    methods: {
        getData() {
            this.seachForm.pageNo = 1
            this.seachData()
        },
        seachData () {
            this.imgList = []
            getEmbroideryList(this.seachForm).then(res => {
                if (res.data.code == 0) {
                    this.total = res.data.data.totalPage
                    res.data.data.data.forEach(item => {
                        if (item.imgUrl !== '' && item.imgUrl !== null) this.imgList.push(item.imgUrl)
                        switch (item.cloType) {
                            case '0':
                                item.cloType = '大袍'
                                break
                            case '1':
                                item.cloType = 'kaften'
                                break
                        }
                    })
                    this.embroTab = res.data.data.data
                }
            })
        },
        handleSizeChange (newSize) {
            this.seachForm.pageSize = newSize
            this.seachData()
        },
        handleCurrentChange (newNum) {
            this.seachForm.pageNo = newNum
            this.seachData()
        },
        getSrcList(index) {
            return this.imgList.slice(index).concat(this.imgList.slice(0, index))
        },
        // 上传文件超出个数触发
        scotland() {
            this.$message.warning('只能选择一个文件')
        },
        handleImg(res) {
            if (res.code == 0) {
                switch (this.doType) {
                    case 1:
                        this.addDataForm.imgUrl = res.data
                        break
                    case 2:
                        this.modifyDataForm.imgUrl = res.data
                        break
                }
            }
        },
        handleRemove(file) {
            if (file) {
                switch (this.doType) {
                    case 1:
                        this.fileList = []
                        this.addDataForm.imgUrl = ''
                        break
                    case 2:
                        this.fileList = []
                        this.modifyDataForm.imgUrl = ''
                        break
                }
            }
        },
        // 获取面料数据
        getFabGroup() {
            getGroupTree({ embroideryId: '' }).then(res => {
                if (res.data.code == 0) {
                    this.fabGroup = res.data.data
                }
            })
        },
        // 获取未操作绣花数据
        getNotEmb() {
            getEmbroideryNotInElement(JSON.stringify({})).then(res => {
                if (res.data.code == 0) {
                    this.embGrooup = res.data.data
                }
            })
        },
        showAdd() {
            this.doType = 1
            this.getNotEmb()
            this.getFabGroup()
            this.addDialogVisible = true
        },
        addDlogClose (form) {
            this.$refs.sunsetstrip.clearFiles()
            this.$refs[form].resetFields()
            this.embItemId = ''
            this.fileList = []
            this.addDataForm.cloType = ''
            this.modelName = ''
        },
        setAddIdAndName(data) {
            this.addDataForm.name = data.split('-')[1]
            this.addDataForm.id = data.split('-')[0]
        },
        checkModel() {
            this.seachModelData()
            this.modelDialogVisible = true
        },
        addData (form) {
            this.$refs[form].validate(valid => {
                if (!valid) return
                if (this.addDataForm.imgUrl == '') return this.$message.warning('请至少选择一张图片上传')
                addEmbroidery(JSON.stringify(this.addDataForm)).then(res => {
                    if (res.data.code == 0) {
                        this.$message.success('添加成功')
                        this.addDialogVisible = false
                        this.seachData()
                    }
                })
            })
        },
        showModify (data) {
            this.doType = 2
            this.getFabGroup()
            this.modifyDataForm.id = data.id
            this.modifyDataForm.underlayFabId = data.underlayFabId
            this.modifyDataForm.imgUrl = data.imgUrl
            switch (data.cloType) {
                case '大袍':
                    this.modifyDataForm.cloType = '0'
                    break
                case 'kaften':
                    this.modifyDataForm.cloType = '1'
                    break
            }
            this.modifyDataForm.patternId = data.patternId
            this.modelName = data.patternName
            this.modifyDataForm.patternId = data.patternId
            if (data.imgUrl !== '' && data.imgUrl !== null) this.fileList.push({ name: data.name, url: data.imgUrl })
            this.modifyDataForm.attribute = data.attribute
            this.modifyDialogVisible = true
        },
        modifyDlogClose(form) {
            this.fileList = []
            this.$refs.sunsetstrip.clearFiles()
            this.$refs[form].resetFields()
        },
        modifyData(form) {
            this.$refs[form].validate(valid => {
                if (!valid) return
                if (this.modifyDataForm.imgUrl == '') return this.$message.warning('请至少选择一张图片上传')
                updateEmbroidery(JSON.stringify(this.modifyDataForm)).then(res => {
                    if (res.data.code == 0) {
                        this.$message.success('修改成功')
                        this.modifyDialogVisible = false
                        this.seachData()
                    }
                })
                this.modifyDialogVisible = false
            })
        },
        showDel(id) {
            this.delItemId = id
            this.delDialogVisible = true
        },
        delData() {
            delEmbroidery(this.delItemId).then(res => {
                if (res.data.code == 0) {
                    this.$message.success(this.$t('delsuc'))
                    this.delDialogVisible = false
                    this.seachData()
                }
            })
        },
        // 分配面料
        setItemEmb(id) {
            getFabricWithEmbroidery(JSON.stringify({ id: id })).then(res => {
                if (res.data.code == 0) {
                    res.data.data.forEach(item => {
                        if (item.fabrics == null) item.isPar = true
                        if (item.fabrics !== null) item.isPar = false
                        switch (item.isPar) {
                            case true:
                                break
                            case false:
                                item.fabrics.forEach(one => {
                                    switch (one.hasBinding) {
                                        case 0:
                                            break
                                        case 1:
                                            this.selectEmbId.push([item.id, one.id])
                                            break
                                    }
                                })
                                break
                        }
                    })
                    this.embForm = res.data.data
                    this.editEmbForm.id = id
                }
            })
            this.embDialogVisible = true
        },
        editemb() {
            switch (this.selectEmbId.length) {
                case 0:
                    this.editEmbForm.fabricIds = []
                    break
                default:
                    this.selectEmbId.forEach(item => {
                        this.editEmbForm.fabricIds.push(item[1])
                    })
                    break
            }
            distributionFabric(JSON.stringify(this.editEmbForm)).then(res => {
                if (res.data.code == 0) {
                    this.$message.success('分配成功')
                    this.embDialogVisible = false
                }
            })
        },
        embDialogVisibleClosed() {
            this.selectEmbId = []
            this.editEmbForm.fabricIds = []
        },
        // kaften版型
        seachModelData() {
            this.imgModelList = []
            getRobesElementAll(this.seachModelForm).then(res => {
                this.total = res.data.data.hits
                res.data.data.data.forEach(item => {
                    this.imgList.push(item.imgUrl)
                })
                this.collarTab = res.data.data.data
            })
        },
        getModelData() {
            this.seachModelForm.pageNo = 1
            this.seachModelData()
        },
        getModelSrcList(index) {
            return this.imgModelList.slice(index).concat(this.imgModelList.slice(0, index))
        },
        handleModelSizeChange (newSize) {
            this.seachModelForm.pageSize = newSize
            this.seachModelData()
        },
        handleModelCurrentChange (newNum) {
            this.seachModelForm.pageNo = newNum
            this.seachModelData()
        },
        handleModelChange(row) {
            if (!row) return
            switch (this.doType) {
                case 1:
                    this.addDataForm.patternId = row.uuid
                    this.modelName = row.name
                    break
                case 2:
                    this.modifyDataForm.patternId = row.uuid
                    this.modelName = row.name
                    break
            }
        }
    },
    mounted () {
        this.seachData()
    }
}
</script>

<style lang="less" scoped>
@import './embroiderlib.less';
</style>
